import { PLAY_MODE } from '@/assets/js/constant'
import { shuffle } from '@/assets/js/util'
export function selectPlay({ commit }, { list, index }) { // 顺序播放
  commit('setPlayMode', PLAY_MODE.sequence)
  commit('setSquenceList', list)
  commit('setPlayingState', true)
  commit('setfullScreen', true)
  commit('setPlaylist', list)
  commit('setCurrentIndex', index)
}

export function randomPlay({ commit }, list) {
  commit('setPlayMode', PLAY_MODE.random) // 随机播放
  commit('setSquenceList', list) // 歌曲列表
  commit('setPlayingState', true) // 播放状态
  commit('setfullScreen', true) // 播放 展示
  commit('setPlaylist', shuffle(list)) // 播放歌曲列表
  commit('setCurrentIndex', 0)
}

export function changeMode({ commit, state, getters }, mode) { // 改成随机播放
  const currentId = getters.currentSong.id // 获取到当前播放歌曲的id
  if (mode === PLAY_MODE.random) { // 随机播放
    commit('setPlaylist', shuffle(state.sequenceList))
  } else { // 顺序播放 循环播放
    commit('setPlaylist', state.sequenceList)
  }
  const index = state.playlist.findIndex((song) => {
    return song.id === currentId
  })
  commit('setCurrentIndex', index)
  commit('setPlayMode', mode)
}
